<?php

	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	$spc = $_GET['spc'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	//获取已经完结的和未完结的ID并生成链接
	$wjcontent = "http://xs.178.com/tags/js/yiwanjie.js";
	$lzcontent = "http://xs.178.com/tags/js/lianzaizhong.js";
	
	$ccc = file_get_contents($wjcontent);
	$ccc = str_replace('\/','',$ccc);
	//echo $ccc;exit;
	$match_str = "/lnovel_url\":\"(.*)index.shtml/isU";
	preg_match_all($match_str,$ccc,$urln);
	$wjids = $urln[1];
	
	$ccc = file_get_contents($lzcontent);
	$ccc = str_replace('\/','',$ccc);
	//echo $ccc;exit;
	$match_str = "/lnovel_url\":\"(.*)index.shtml/isU";
	preg_match_all($match_str,$ccc,$urlnn);
	$lzids = $urlnn[1];
	
	
	
	//开始整站采集
	if($spc == 'wj'){
		getwj($wjids);
	}else if($spc == 'lz'){
		getlz($lzids);
	}else if($spc == 'all'){
		getlz($lzids);
		getwj($wjids);
	}
	
	echo "采集完毕";
	ob_end_flush();
	
	
	//以下为函数定义
	function getwj($ids){
		foreach ($ids as $tv) {
			getsig($tv);
		}
	}
	
	function getlz($ids){
	foreach ($ids as $tv) {
			getsig($tv);
		}
	}
	
	function getsig($tid){
		global $DB;
		$url = 'http://xs.178.com/' . $tid . '/index.shtml';
		$id = $tid;
		$url_pre = $url;
		$pic_url_pre = 'http://xs.178.com/';
		$content = 	file_get_contents($url);
		$name = explode('var g_lnovel_name = \'',$content);
		$name = $name[1];
		$name = explode('\'',$name);
		$name = $name[0];
		$dt = explode('更新时间：',$content);
		$dt = $dt[1];
		$dt = explode('</span>',$dt);
		$dt = strtotime(trim($dt[0]));
		if ($dt <= 0) {
			$dt = 0;
		}
		$urls = explode('<!--novel_cover_box-->',$content);
		$urls = $urls[1];
		$urls = explode('<div class="probable_like">',$urls);
		$urls = $urls[0];
		$urls = explode('<div class="clearfix"></div>',$urls);
		unset($urls[0]);
		unset($urls[count($urls)]);
		$numn = count($urls);
		$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => '178_tags','where'	=> "1 " . $where ,)
		);	
		if ($result['max'] > 0) {}
		else{
			do_taginsert($id,$dt,$name,0);
		}
		$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'count','from'   => '178_tags','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['count'];
		
		
		foreach ($urls as $k=>$v) {
			if ($k <= $dbcount) {
					continue;
			}
			//continue;
			$utf8dl = explode('">下载1',$v);
			$utf8dl = explode('<a href=".',$utf8dl[0]);
			$utf8dl = $utf8dl[1];
			$utf8dl = $pic_url_pre . $tid . $utf8dl;
			$gbdl = str_replace(".txt","_GBK.txt",$utf8dl);
			$cname = explode('点此下载TXT</a></span>',$v);
			$cname = explode('</li>',$cname[1]);
			$cname = $cname[0];
			do_photoinsert($id,$utf8dl,$gbdl,$cname);
		}

		$where = 'id= ' . $id .'';
		$DB->do_update('178_tags',
			array(					  
			   'lastupdate'				=> $dt,
			   'count'					=> count($urls),
			 ),
			 $where
		);
		echo $name . "采集完毕<br>";
	}
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( '178_tags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}

	function do_photoinsert($uid,$url,$gburl,$name){
		global $DB;
		$DB->do_insert( '178_photos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'gburl'					=> $gburl,								
								   'name'					=> $name,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	
?>